﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;

namespace MainWebService
{
    /// <summary>
    /// Summary description for Service1
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class UserService : System.Web.Services.WebService
    {
        [WebMethod]
        public UserDetails ValidateUser(string username, string password,string deviceId,string deviceName)
        {
            UserDetails _user = new UserDetails();
            try
            {
                _user.UserName = username;
                _user.Password = password;
                
                _user = _user.ValidateUser(deviceId,deviceName);
            }
            catch (Exception ex)
            {
                _user.LoginMessage = ex.Message;
            }

            return _user;
        }

        public string AddUser(string emailId, string appUserName, string password, string company)
        {
            UserDetails _usr = new UserDetails();
            string msgs = "";
            try
            {
                _usr.EmailId = emailId;
                _usr.UserName = appUserName;
                _usr.Password = password;
                _usr.Company = company;
                msgs = _usr.AddUser();
                _usr.LoginMessage = msgs;
            }
            catch (Exception ex)
            {
                _usr.LoginMessage = ex.Message;
            }
            return msgs;
        }
        [WebMethod]
        public string AddUser(int userId, string emailId, string appUserName, string password, string company, int deviceLimit, int clientId, List<string> lstServices, string hashCode)
        {
            UserDetails _usr = new UserDetails();
            string msgs = "";
            try
            {
                _usr.UserId = userId;
                _usr.EmailId = emailId;
                _usr.UserName = appUserName;
                _usr.Password = password;
                _usr.Company = company;
                _usr.ClientId = clientId;
                _usr.DeviceLimit = deviceLimit;
                _usr.ServiceList = lstServices;
                _usr.HashCode = hashCode;
                msgs = _usr.AddUser();
                _usr.LoginMessage = msgs;
            }
            catch (Exception ex)
            {
                msgs = ex.Message;
            }
            return msgs;
        }

        [WebMethod]
        public string RegisterUser(UserDetails objUser)
        {
            string msgs = "";
            try
            {
                UserOperations _usr = new UserOperations();
                msgs= _usr.RegisterUser(objUser);
               // msgs = objUser.AddUser();
               // objUser.LoginMessage = msgs;
            }
            catch (Exception ex)
            {
                msgs = ex.Message;
            }
            return msgs;
        }
        [WebMethod]
        public string UpdateUser(int userId, string appUserName, string password, int deviceLimit, List<string> lstServices, int clientId, string hashCode)
        {
            UserDetails _usr = new UserDetails();
            string msgs = "";
            try
            {
                _usr.UserId = userId;
                _usr.EmailId = "";
                _usr.HashCode = hashCode;
                _usr.UserName = appUserName;
                _usr.Password = password;
                _usr.Company = "";
                _usr.ClientId = clientId;
                _usr.DeviceLimit = deviceLimit;
                _usr.ServiceList = lstServices;
                msgs = _usr.UpdateUser();
                _usr.LoginMessage = msgs;
            }
            catch (Exception ex)
            {
                msgs = ex.Message;
            }
            return msgs;
        }

        [WebMethod]
        public string DeleteUser(int userId, int clinetId, string hashCode)
        {
            UserDetails _usr = new UserDetails();
            string msgs = "";
            try
            {
                _usr.UserId = userId;
                _usr.HashCode = hashCode;
                _usr.ClientId = clinetId;
                msgs = _usr.DeleteUser();
                _usr.LoginMessage = msgs;
            }
            catch (Exception ex)
            {
                msgs = ex.Message;
            }
            return msgs;
        }

        [WebMethod]
        public List<UserDetails> ListUsers(int clientId, string HashCode)
        {
            return UserDetails.GetUserList(clientId, HashCode);
        }

        [WebMethod]
        public UserDetails GetAppUser(int userId, int clientId, string hashCode)
        {
            return UserDetails.ListAppUser(userId, clientId, hashCode);
        }

        [WebMethod]
        public System.Data.DataSet GetUsers(int userId, string hashCode)
        {
            return UserDetails.GetUsers(userId, hashCode);
        }

        [WebMethod]
        public void AssociateServiceToUsers(int UserId, string HashCode, int ServiceId, string usersList)
        {
            UserDetails.AssociateService(UserId, HashCode, ServiceId, usersList);
        }

        [WebMethod]
        public DataSet GetServicesList(int clientId, string hashCode)
        {
            return UserDetails.GetServicesList(clientId,hashCode);
        }

        [WebMethod]
        public DataSet GetAppUsers(int clientId, string hashCode)
        {
            return UserDetails.GetAppUsers(clientId, hashCode);
        }
        [WebMethod]
        public DataSet GetDevices(int clientId, string hashCode)
        {
            return UserDetails.GetDevices(clientId, hashCode);
        }

        [WebMethod]
        public string ActivateUser(int userId,string hash)
        {
            return UserDetails.Activate(userId, hash);
        }

        [WebMethod]
        public string UserSecurity(string email,string type)
        {
            return UserDetails.GetUserSecurity(email, type);
        }
        [WebMethod]
        public string GetEmail(string token)
        {
            return UserDetails.GetEmail(token);
        }
        [WebMethod]
        public  string ResetPassword(string token,string newPassword)
        {
            try
            {
                return UserDetails.ResetPassword(token, newPassword);
            } catch(Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        [WebMethod]
        public DataSet GetAccountUsers(string userId)
        {
            return UserDetails.GetAccountUsers(userId);
        }

        [WebMethod]
        public string UpdateUserAccount(UserDetails objuser)
        {
            return new UserOperations().UpdateUserAccount(objuser);
        }

        [WebMethod]
        public string DeleteUserAccount(string username,string password,int userId,string hashCode)
        {
            return new UserOperations().DeleteAccount(username, password, userId, hashCode);
        }
        [WebMethod]
        public string ChangePassword(int userId,string password,string hashCode)
        {
            return new UserOperations().ChangePassword(userId, password, hashCode);
        }

        [WebMethod]
        public DataSet GetAccountsDevices(int clientId,string hashCode)
        {
            return new UserOperations().GetDeivces(clientId, hashCode);
        }

        [WebMethod]
        public string ArchiveDataAndSendMail()
        {
            return new UserOperations().ArchiveAndSendMail();
        }
        [WebMethod]
        public string AddComment(int commentId,int CommentUserId,string CommentDesc,int loggedinUser)
        {
            return new UserOperations().AddComment(commentId, CommentUserId, CommentDesc, loggedinUser);
        }
        [WebMethod]
        public DataSet GetComments(int commentedUserId)
        {
            return new UserOperations().GetComments(commentedUserId);
        }
        [WebMethod]
        public string DeleteComment(int commentId)
        {
            return new UserOperations().DeleteComment(commentId);
        }

        [WebMethod]
        public string GetEmailIds(string userId)
        {
            return new UserOperations().GetEmailIds(userId);
        }
        [WebMethod]
        public string AddEmailIds(string userId,string loggedinuser,string emailids,string hashCode)
        {
            return new UserOperations().AddEmailIds(userId,hashCode,emailids,loggedinuser);
        }
    }
}